package com.llxwz.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.llxwz.dao.ReportMapper;
import com.llxwz.dao.UserMapper;
import com.llxwz.entity.Report;
import com.llxwz.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private UserMapper userMapper;

    public Report findReportById(int id){
        return reportMapper.selectById(id);
    }


    public int getReportRows(String targetUser,String username,int entityType,int status){
        QueryWrapper result = new QueryWrapper();
        if (!"null".equals(targetUser)){
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("username",targetUser);
            User user = userMapper.selectOne(wrapper);
            result.eq("target_user_id",user.getId());
        }
        if (!"null".equals(username)){
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("username",username);
            User user = userMapper.selectOne(wrapper);
            result.eq("user_id",user.getId());
        }
        if(entityType!=-1){
            result.eq("entity_type",entityType);
        }
        if(status!=-1){
            result.eq("status",status);
        }
        return reportMapper.selectCount(result);
    }

    public List<Report> getReportList(String targetUser,String username,
                                      int entityType,int status,
                                      int current, int limit){
        QueryWrapper result = new QueryWrapper();
        if (!"null".equals(targetUser)){
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("username",targetUser);
            User user = userMapper.selectOne(wrapper);
            result.eq("target_user_id",user.getId());
        }
        if (!"null".equals(username)){
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("username",username);
            User user = userMapper.selectOne(wrapper);
            result.eq("user_id",user.getId());
        }
        if(entityType!=-1){
            result.eq("entity_type",entityType);
        }
        if(status!=-1){
            result.eq("status",status);
        }
        Page<Report> page = reportMapper.selectPage(new Page<>(current,limit), result);
        return page.getRecords();
    }

    public int updateStatus(int id, int status) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("status",status);
        return reportMapper.update(null,updateWrapper);
    }

    public int updateEntityType(int id, int entityType) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("entity_type",entityType);
        return reportMapper.update(null,updateWrapper);
    }


    public int addReport(Report report){
        return reportMapper.insert(report);
    }

}
